package org.jarraiders.musiclibrary.server.utils;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.ejb.Ejb3Configuration;

@SuppressWarnings("deprecation")
public class HibernateUtil {
	public static final Log logger = LogFactory.getLog(HibernateUtil.class
			.getName());

	private static final SessionFactory sessionFactory;
	private static final Ejb3Configuration ejb3Configuration;
//	private static final EntityManager entityManager;

	static {
		try {
			// Create the SessionFactory from hibernate.cfg.xml
			sessionFactory = new AnnotationConfiguration().configure()
					.buildSessionFactory();

			ejb3Configuration = new Ejb3Configuration()
					.configure("hibernate.cfg.xml");

			/*entityManager = ejb3Configuration.buildEntityManagerFactory()
					.createEntityManager();
*/
		} catch (Throwable ex) {
			logger.error("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	// public static Ejb3Configuration getEjb3Configuration() {
	// return ejb3Configuration;
	// }

	// public static EntityManager getEntityManager() {
	// return entityManager;
	// }

	/*public static EntityTransaction getEntityManagerTransaction() {
		return entityManager.getTransaction();
	}*/
}
